class cutPage {
    constructor(obj) {
        this.el = this.getDom(obj.el);
        this.data = obj.data;
        this.pageLength = obj.pageLength;
        this.select = this.getDom('select', this.el);
        this.total = this.getDom('.total', this.el);
        this.next = this.getDom('.next', this.el);
        this.prev = this.getDom('.prev', this.el);
        this.pageBtn = this.getDom('.page-btn', this.el);
        this.cb2 = obj.cb2;
        this.cb1 = obj.cb1;
        this.init();
    }
    init() {
            this.limit = this.pageLength[0];
            this.pagenum = 0;
            this.getDom();
            this.Renderselect();
            this.RenderBtn();
            this.selectChange();
            this.bindEvent();
            this.getData();
        }
        //获取节点
    getDom(str, parent = document) {
            return typeof str === 'string' ? parent.querySelector(str) : str;
        }
        //渲染下拉框
    Renderselect() {
            let str = '';
            this.pageLength.forEach(itme => {
                str += `<option value=${itme}>${itme}/条/页</option>`;
            })
            this.select.innerHTML = str;
        }
        //渲染分页按钮
    RenderBtn() {
            let size = this.size = Math.ceil(this.data.length / this.limit);
            this.cb1(size);
        }
        //改变下拉值
    selectChange() {
            this.select.onchange = () => {
                this.pageBtn.innerHTML = "";
                this.pagenum = 0;
                this.limit = this.select.value * 1;
                this.RenderBtn();
                this.getData();
            }
        }
        //点击分页按钮以及上下也
    bindEvent() {
            this.pageBtn.onclick = (e) => {
                let target = e.target;
                if (target.tagName === 'SPAN') {
                    this.pagenum = target.innerHTML - 1;
                    this.getData();
                }
            }
            this.prev.onclick = () => {
                this.pagenum--;
                this.pagenum = this.pagenum <= 0 ? 0 : this.pagenum;
                this.getData();
            }
            this.next.onclick = () => {
                this.pagenum++;
                this.pagenum = this.pagenum >= this.size - 1 ? this.size - 1 : this.pagenum;
                this.getData();
            }
        }
        //获取数据
    getData() {
        let start = this.limit * this.pagenum;
        let end = this.limit + start;
        let btns = Array.from(this.pageBtn.children);
        for (let i = 0; i < btns.length; i++) {
            btns[i].classList.remove('active');
        }
        btns[this.pagenum].classList.add('active');
        this.cb2(this.data.slice(start, end));
    }
}